import time
import unittest

from app.base_app import BaseApp
from app.core.log import Log
from app.hepler.helper import root_path
from app.lib.HTMLTestRunner import HTMLTestRunnerCN


class Xatom(BaseApp):
    def __init__(self):
        super().__init__()

        self.testcases = ""

        # 接口测试路径
        self.testcase_path = root_path() + "/" + self.config.get("testcase", "folder")

        # report文件名
        now = time.strftime("%Y-%m-%d %H:%M:%S")
        self.report_filename = root_path() + '/' + self.config.get("report", "folder") + "/" + now + '-result.html'

    def run(self):
        self.add_case()

        self.run_case()

    def add_case(self):
        """加载所有的测试用例"""

        discover = unittest.defaultTestLoader.discover(self.testcase_path, pattern='*.py')
        self.testcases = discover

    def run_case(self):
        """执行所有的测试用例"""

        # 1. HTMLTestRunner
        with open(self.report_filename, "wb") as f:
            runner = HTMLTestRunnerCN.HTMLTestReportCN(stream=f, title='xatom接口自动化测试报告',
                                                       description='环境：mac 浏览器：chrome')
            runner.run(self.testcases)

        # 2. 发送测试报告
        pass


if __name__ == "__main__":
    x = Xatom()
    x.run()
